﻿using UnityEngine;
using System.Collections;

public class CharacterControl : MonoBehaviour {

	float maxSpeed = 9f;
	bool facingRight = true;
	Animator anim;

	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		float move = Input.GetAxis ("Horizontal");
		rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);

		anim.SetFloat ("speed", Mathf.Abs (move));

		if (move > 0 && !facingRight) {
			Flip();
		} else if (move<0 && facingRight) {
			Flip ();
		}

	}

	void Flip() {
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
		//It flips it about the top left corner
		Vector3 thePos = transform.localPosition;
		thePos.x -= theScale.x;
		transform.localPosition = thePos;
	}
}
